
import { _decorator } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
const { ccclass, property } = _decorator;
/**传送门 */
@ccclass('Portal')
export class Portal extends yyComponent {

    public init() {
        this.visible = true;
        this.curRole = null;
        this.nextDoor = null;
        this.registAllCustomUpdate();
    }
    public reset() {
        this.visible = true;
        this.curRole = null;
        this.nextDoor = null;
    }

    protected registAllCustomUpdate() {
        this.registCustomUpdate(State.leaving, this.stepRoleLeaving);
    }

    public checkCollRole(role) {
        let p0 = role.node.position;
        let p1 = this.node.position;
        let x = p0.x - p1.x;
        let z = p0.z - p1.z;
        return Math.abs(x) < 0.8 && Math.abs(z) < 0.8;
    }
    public chuanSongRole(role) {
        this.curRole = role;
        this.visible = false;
        this.enterCustomUpdateState(State.leaving);
    }

    /**是否能传送 */
    public visible: boolean = true;
    /**当前正在传送的角色 */
    public curRole: yyComponent = null;
    //#region 连接的下一扇门
    public nextDoor: Portal = null;
    //#endregion

    /**传送完成，等待角色离开后传送功能再次生效 */
    protected stepRoleLeaving(dt: number) {
        if (!this.curRole || !this.curRole.isAlive) {
            this.curRole = null;
            this.visible = true;
            this.enterCustomUpdateState(State.closed);
            return;
        }
        if (!this.curRole.canChuanSong) {
            return;
        }
        let x = this.node.position.x - this.curRole.node.position.x;
        let z = this.node.position.z - this.curRole.node.position.z;
        if (Math.abs(x) > 1.2 || Math.abs(z) > 1.2) {
            this.curRole = null;
            this.enterCustomUpdateState(State.closed);
            this.visible = true;
        }
    }

}

enum State {
    closed,
    opening,
    opened,
    closing,
    leaving,
}